package com.example.coursedesign.ui.home.dao;

import android.util.Log;

import com.example.coursedesign.ui.home.entity.UserData;
import com.example.coursedesign.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDataDao {

    private static final String TAG = "mysql-as_coursedesign-UserDataDao";

    /**
     * 保存用户数据
     */
    public long saveUserData(UserData userData) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet generatedKeys = null;
        long newRowId = -1;

        try {
            // 获取数据库连接
            connection = JDBCUtils.getConn();
            if (connection == null) {
                throw new SQLException("无法获取数据库连接");
            }

            // 开启事务
            connection.setAutoCommit(false);

            // 插入用户数据的 SQL 语句
            String sql = "INSERT INTO userdata (account, blood_pressure, blood_sugar, height, weight, heart_rate, physical_exam_report) VALUES (?, ?, ?, ?, ?, ?, ?)";
            ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);

            // 设置 PreparedStatement 参数
            ps.setString(1, userData.getAccount());
            ps.setString(2, userData.getBloodPressure());
            ps.setDouble(3, userData.getBloodSugar());
            ps.setDouble(4, userData.getHeight());
            ps.setDouble(5, userData.getWeight());
            ps.setInt(6, userData.getHeartRate());
            ps.setString(7, userData.getPhysicalExamReport());

            // 执行插入操作
            int rowsAffected = ps.executeUpdate();
            if (rowsAffected > 0) {
                // 获取自动生成的主键
                generatedKeys = ps.getGeneratedKeys();
                if (generatedKeys.next()) {
                    newRowId = generatedKeys.getLong(1);
                }
            }

            // 提交事务
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            Log.e(TAG, "保存用户数据失败: " + e.getMessage());

            // 回滚事务
            if (connection != null) {
                try {
                    connection.rollback();
                } catch (SQLException rollbackEx) {
                    rollbackEx.printStackTrace();
                    Log.e(TAG, "事务回滚失败: " + rollbackEx.getMessage());
                }
            }
        } finally {
            // 关闭资源
          // JDBCUtils.closeConnection(connection, ps, generatedKeys);
        }

        return newRowId;
    }

    /**
     * 根据 ID 获取用户数据
     */
    public UserData getUserDataById(int id) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        UserData userData = null;

        try {
            // 获取数据库连接
            connection = JDBCUtils.getConn();
            if (connection == null) {
                throw new SQLException("无法获取数据库连接");
            }

            // 查询用户数据的 SQL 语句
            String sql = "SELECT * FROM userdata WHERE id = ?";
            ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();

            // 处理查询结果
            if (rs.next()) {
                userData = new UserData();
                userData.setId(rs.getInt("id"));
                userData.setAccount(rs.getString("account"));
                userData.setBloodPressure(rs.getString("blood_pressure"));
                userData.setBloodSugar(rs.getDouble("blood_sugar"));
                userData.setHeight(rs.getDouble("height"));
                userData.setWeight(rs.getDouble("weight"));
                userData.setHeartRate(rs.getInt("heart_rate"));
                userData.setPhysicalExamReport(rs.getString("physical_exam_report"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            Log.e(TAG, "获取用户数据失败: " + e.getMessage());
        } finally {
            // 关闭资源
           // JDBCUtils.closeConnection(connection, ps, rs);
        }

        return userData;
    }

    // 你可以根据需要添加更多的CRUD方法，如更新和删除用户数据
}